import xlrd
import pyecharts.options as opts
data=xlrd.open_workbook(r'C:\Users\chumenliuxiang\Desktop\实验室作业\第一问\第一问.xls','r')
sheet_num=data.nsheets
# print(sheet_num)
biao1=data.sheet_by_name('Sheet1')
# print(biao1)
hours=biao1.col_values(0)
hours=hours[1:]
h0=h1=h2=h3=h4=h5=h6=h7=h8=h9=0
for i in hours:
    if i<=1:
        h0=h0+1
    elif i<=2:
        h1+=1
    elif i<=3:
        h2+=1
    elif i<=4:
        h3+=1
    elif i<=5:
        h4+=1
    elif i<=6:
        h5+=1
    elif i<=7:
        h6+=1
    elif i<=8:
        h7+=1
    elif i<=9:
        h8+=1
    elif i<=10:
        h9+=1
student1=[]
student1.append(h0)
student1.append(h1)
student1.append(h2)
student1.append(h3)
student1.append(h4)
student1.append(h5)
student1.append(h6)
student1.append(h7)
student1.append(h8)
student1.append(h9)
# print(student1)
#——————————————————————数据处理完毕
from pyecharts import options as opts
from pyecharts.charts import PictorialBar
from pyecharts.globals import SymbolType

hour = ["学习0~1小时", "学习1~2小时", "学习2~3小时", "学习3~4小时", "学习4~5小时", "学习5~6小时", "学习6~7小时", "学习7~8小时", "学习8~9小时", "学习9~10小时"]
values = student1

(
    PictorialBar()
    .add_xaxis(hour)
    .add_yaxis(
        "",
        values,
        label_opts=opts.LabelOpts(is_show=False),
        symbol_size=18,
        symbol_repeat="fixed",
        symbol_offset=[0, 0],
        is_symbol_clip=True,
        symbol=SymbolType.ROUND_RECT,
    )
    .reversal_axis()
    .set_global_opts(
        title_opts=opts.TitleOpts(title="学习时长直方图",pos_left='center',pos_bottom='1%'),
        xaxis_opts=opts.AxisOpts(is_show=False),
        yaxis_opts=opts.AxisOpts(
            axistick_opts=opts.AxisTickOpts(is_show=False),
            axisline_opts=opts.AxisLineOpts(
                linestyle_opts=opts.LineStyleOpts(opacity=0)
            ),
        ),
    )
    .render("第二问.html")
)